#!/usr/bin/env bash

read -r -d '' ASANFLAGS <<- EOF
-fsanitize=thread
EOF
ASANFLAGS="$(echo "${ASANFLAGS}" | tr '\n' ' ')"

: ${BUILD_DIR="clang-tsan-build"}
: ${R_HOME="$(R RHOME)"}

mkdir -p "${BUILD_DIR}"
cd "${BUILD_DIR}"
cmake ../cpp                                                \
    -DLIBR_HOME="${R_HOME}"                                 \
    -DCMAKE_C_COMPILER=/opt/homebrew/opt/llvm/bin/clang     \
    -DCMAKE_CXX_COMPILER=/opt/homebrew/opt/llvm/bin/clang++ \
    -DCMAKE_BUILD_TYPE="Debug"                              \
    -DCMAKE_C_FLAGS="${ASANFLAGS}"                          \
    -DCMAKE_CXX_FLAGS="${ASANFLAGS}"                        \
    "$@"
cd ..

cmake --build "${BUILD_DIR}"
